package com.neuedu.helloworld.concat;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.neuedu.helloworld.R;

import java.io.Serializable;
import java.sql.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ConcatEditActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {

    private TextView concatName;

    private ListView lvConcatEdit;

    private List<Map<String,Object>> data = new ArrayList<>();
    private SimpleAdapter adapter;
    HashMap person  = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


//        getActionBar();//不可以用


        ActionBar actionBar = getSupportActionBar();
        //显示后退按钮
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setDisplayShowHomeEnabled(true);



        //设置Activity的布局
        setContentView(R.layout.activity_concat_edit);
        setTitle("联系人编辑");


        concatName = findViewById(R.id.concatName);

        Intent intent = getIntent();
        person = (HashMap) intent.getSerializableExtra("personData");


        lvConcatEdit = findViewById(R.id.lvConcatEdit);



        Map row1 = new HashMap();
        row1.put("key","姓名");
        row1.put("value","张飞");
        row1.put("img",R.drawable.forward_25);
        data.add(row1);


        Map row2 = new HashMap();
        row2.put("key","证件类型");
        row2.put("value","身份证");
//        row2.put("img",R.drawable.forward_25);
        data.add(row2);

        Map row3 = new HashMap();
        row3.put("key","证件号码");
        row3.put("value","12345646546514651");
//        row3.put("img",R.drawable.forward_25);
        data.add(row3);

        Map row4 = new HashMap();
        row4.put("key","乘客类型");
        row4.put("value","学生");
        row4.put("img",R.drawable.forward_25);
        data.add(row4);

        Map row5 = new HashMap();
        row5.put("key","电话");
        row5.put("value","189123456786");
        row5.put("img",R.drawable.forward_25);
        data.add(row5);

        //设置适配器
        String[] from = {"key","value"  ,"img"};
        int[] to = {R.id.tvKey  ,R.id.tvValue , R.id.ivImage   };
        adapter = new SimpleAdapter(this,data,R.layout.activity_concat_edit_item,from,to);
        lvConcatEdit.setAdapter(adapter);

        lvConcatEdit.setOnItemClickListener(this);
    }


    //编辑页，每行点击的时候触发的
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


        switch (position){
            case 0:
                //显示编辑姓名的画面
                showEditName();
                break;
            case 3:
                //显示编辑姓名的画面
                showEditType();
                break;
        }





    }

    //修改乘客类型
    private void showEditType() {


        final String[] singleChoiceItems = {"成人","学生","儿童","其他"};

        AlertDialog dialog = new AlertDialog.Builder(this)
                .setIcon(android.R.drawable.ic_dialog_map)
                .setTitle("请选择")
                .setSingleChoiceItems(singleChoiceItems, 0, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //当选中某一个选项 触发
                        Map row4 =  data.get(3);
                        row4.put("value",singleChoiceItems[which]);

                        adapter.notifyDataSetChanged(); //通知ListView重新渲染

                        dialog.dismiss();//隐藏 关闭dialog
                    }
                })
                //显示Dialog
                .show();

    }


    //显示编辑姓名 的Dialog
    private void showEditName() {

//        AlertDialog

        final EditText et = new EditText(this);
        Map row1 =  data.get(0);
        CharSequence name = (CharSequence) row1.get("value");
        et.setText((CharSequence) row1.get("value"));
        et.setSelection(0,name.length());
        AlertDialog dialog = new AlertDialog.Builder(this)
                .setIcon(android.R.drawable.ic_dialog_info)
                .setTitle("请输入姓名")
                .setView(et)
                //设置左侧按钮
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //点击确定
                    }
                })
                //设置右侧的按钮
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //获取 文本框的内容，设置
                        String name = et.getText().toString();

                        //设置到ListView的数据中
                        /**
                         *  Map row1 = new HashMap();
                     *         row1.put("key","姓名");
                     *         row1.put("value","张飞");
                     *         row1.put("img",R.drawable.forward_25);
                         */
                        Map row1 =  data.get(0);
                        row1.put("value",name);

                        adapter.notifyDataSetChanged(); //通知ListView重新渲染

                    }
                })
                //显示Dialog
                .show();



    }

    /**
     *  true : 事件被消耗，
     *  fasle :  对事件仅仅使用，
     * @param item
     * @return
     */
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {

        switch (item.getItemId()){
            case android.R.id.home:
                finish();
                break;
            case R.id.delConcat:
                //当前ID， 调用后端的接口，
                //回到列表页面，重新加载
//                Map row1 =  data.get(0);
//                CharSequence name = (CharSequence) row1.get("value");
//                Toast.makeText(this, name+"被删除了", Toast.LENGTH_SHORT).show();

                Intent data = new Intent();
                int id = (int) person.get("id");
                data.putExtra("id",id);

                //关闭当前Activity之前，需要设置结果
                setResult(100,data);

                //统一打开本Activity 的地方，告诉他我删除了哪个 ID
                finish();
                break;
        }


//
//        if(item.getItemId()  == android.R.id.home ){
//            finish();
//        }else{
//
//        }

        return super.onOptionsItemSelected(item);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

//        View view  = getLayoutInflater().inflate("xml");

        getMenuInflater().inflate( R.menu.concat_edit_menu   ,menu);

        return super.onCreateOptionsMenu(menu);
    }
}